<?php
##################################################################
//File			: messages.php
//Description	: do the inbox,sent message and other message functions
//Author		: ilayaraja_22ag06
//Created On	: 16-Apr-2007
//Last Modified	: 19-Apr-2007
##################################################################

session_start();//start the session

class Messages extends Controller 
{

	function Intro()
		{
			parent::Controller();	
		}//end method
	
	function index()
		{
			$this->inbox();
		}//end method

		
	function compose()
		{
			$this->load_settings();
			$this->load->helper('smiley');
			$this->load->library('table');
			$datas	= $this->common->authenticate();
			global $userId,$now;				//declare $userId and $now as global variable
			$data		= array();
			if(count($datas)<1)
				{
					$datas	= $this->common->basicvars();
					$data	= $datas;
					$data['siteUrl']		= site_url();
					$data['center_login']	= true;
					$this->smartyextended->view('user/login',$data);
				}
			else
				{
					$data	= $datas;
					
					$data['count_friends_request']	= $this->common->countFriendRequest($userId);
					$data['count_new_msg']	= $this->common->countNewMsg($userId);
					$data['count_out_request']	= $this->common->countOutRequest($userId);					

					$image_array = get_clickable_smileys('http://demo.maax.ws/application/images/smileys/');
					$col_array = $this->table->make_columns($image_array, 22);		
					$data['smiley_table'] = $this->table->generate($col_array);
					$data['insert_smiley'] = js_insert_smiley("send_form", "mailContent");
					
					
					$data['count_friends_request']	= $this->common->countFriendRequest($userId);
					$data['count_new_msg']	= $this->common->countNewMsg($userId);
					
					$res		= mysql_query("SELECT friend_id AS friend_id FROM `friends_list` WHERE user_id='$userId' and approved_status='yes'");
					$receiverType	= array();
					$receiverType[0]	= "--";
					if($res) {
					while($rs=mysql_fetch_object($res)){

					$usrRs		= getRow("users", "firstname, lastname", "user_id='$rs->friend_id'");
					
					$receiverType[''.$usrRs['firstname'].' '.$usrRs['lastname'].'']	= ''.$usrRs['firstname'].' '.$usrRs['lastname'].'';

					}
					$data['ReceiverView']	= selectBox('mailTo', $receiverType,'mailTo','style="width: 100%;"');
					
					} 
					
					if($this->uri->segment(3)>0)//message to particular user
						{
							$data['action_id']	= $this->uri->segment(3);		//group id
							$data['action_type']= 'single_user';						//to indicate,message for group
							$data['action_name']= $this->common->getUsername($this->uri->segment(3));
							$data['action_url']	= base_url().'user/profile/'.$data['action_id'];
						}
					else
						{
							$data['action_id']	= 0;
							$data['action_type']= 'friend';
						}
					$this->smartyextended->view('messages/compose',$data);
				}
		}//end compose
		
	function inbox()
		{
			$this->load_settings();
			$datas	= $this->common->authenticate();
			global $userId,$now;				//declare $userId and $now as global variable
			$data		= array();
			
			if(count($datas)<1)
				{
					$datas	= $this->common->basicvars();
					$data	= $datas;
					$data['siteUrl']		= site_url();
					$data['center_login']	= true;
					$this->smartyextended->view('user/login',$data);
				}
			else
				{
					$data	= $datas;
					
					$data['count_friends_request']	= $this->common->countFriendRequest($userId);
					$data['count_new_msg']	= $this->common->countNewMsg($userId);
					$data['count_out_request']	= $this->common->countOutRequest($userId);

					$query				= "SELECT subject,message,from_id,datestamp,inbox_message_id,message_status
											FROM inbox_messages 
											WHERE user_id='$userId' ORDER BY datestamp DESC";
					$data['content']	= $this->common->listMessage($query,'inbox');
					$data['inbox']		= true;

					$this->smartyextended->view('messages/inbox',$data);
				}
		}//end inbox

		
	function sent()
		{
			$this->load_settings();
			$datas	= $this->common->authenticate();
			global $userId,$now;				//declare $userId and $now as global variable
			$data		= array();
			
			if(count($datas)<1)
				{
					$datas	= $this->common->basicvars();
					$data	= $datas;
					$data['siteUrl']		= site_url();
					$data['center_login']	= true;
					$this->smartyextended->view('user/login',$data);
				}
			else
				{
					$data		= $datas;
					
					$data['count_friends_request']	= $this->common->countFriendRequest($userId);
					$data['count_new_msg']	= $this->common->countNewMsg($userId);
					$data['count_out_request']	= $this->common->countOutRequest($userId);

					$query		= 	"SELECT subject,to_id ,datestamp,message_id,message, message_status, to_name, to_type 
									FROM messages 
									WHERE user_id='$userId' ORDER BY datestamp DESC";
					$data['content']	= $this->common->listMessage($query,'sent');
					$data['sent']		= true;
					$this->smartyextended->view('messages/inbox',$data);
				}
		}//end sent

			
	function read()
		{
			$this->load_settings();
			$this->load->helper('typography');
			$this->load->helper('smiley');
			$this->load->library('table');
			$datas	= $this->common->authenticate();
			global $userId,$now;				//declare $userId and $now as global variable
			$data		= array();
			if(count($datas)<1)
				{
					$datas	= $this->common->basicvars();
					$data	= $datas;
					$data['siteUrl']		= site_url();
					$data['center_login']	= true;
					$this->smartyextended->view('user/login',$data);
				}
			else
				{
					$data		= $datas;
					
					$image_array = get_clickable_smileys('http://demo.maax.ws/application/images/smileys/');
					$col_array = $this->table->make_columns($image_array, 20);		
					$data['smiley_table'] = $this->table->generate($col_array);
					$data['insert_smiley'] = js_insert_smiley("send_form", "mailContent");
					
					$data['count_friends_request']	= $this->common->countFriendRequest($userId);
					$data['count_new_msg']	= $this->common->countNewMsg($userId);
					$data['count_out_request']	= $this->common->countOutRequest($userId);

					if($this->uri->segment(3)!='')
						{
							$msgIdSplit	= explode("_",$this->uri->segment(3));
							$msgId		= $msgIdSplit[1];
							if($msgIdSplit[0]=='s')
								{
									$backUrl	= '<a href="'.base_url().'messages/sent">&larr; повернутись до надісланих</a>';
									mysql_query("UPDATE messages SET message_status='read' WHERE message_id='$msgId'");
									
									$rs	= getRow("messages","user_id,to_id,subject,message,datestamp","message_id='$msgId'");
									$userImage	= $this->common->getAvatar($rs['to_id']);
									$userName	= explode(" ", $this->common->getUsername($rs['to_id']));
									$actualDate	= strtotime($rs['datestamp']);
									$sentDate	= date('d.m.Y',$actualDate);
									$sentTime	= date('h:i',$actualDate);
									

									$data['action_id']	= $rs['to_id'];		//group id
									$data['action_type']= 'single_user';						//to indicate,message for group
									$data['action_name']= $this->common->getUsername($rs['to_id']);
									$data['action_url']	= base_url().'user/profile/'.$data['action_id'];
									$data['action_subject'] = $rs['subject'];
									
									$profileUrl	= base_url().'user/profile/'.$rs['from_id'];	
								
									$content	= '
													<table class="tabs" cellpadding="0" cellspacing="0">
													<tr>
													<td class="tab0">&nbsp;</td>
													<td class="tab2" NOWRAP><a href="'.base_url().'messages/inbox">Отримані</a></td>
													<td class="tab">&nbsp;</td>
													<td class="tab1" NOWRAP><a href="'.base_url().'messages/sent">Надіслані</a></td>
													<td class="tab3">&nbsp;</td>
													</tr>
													</table>		
													
													<img src="'.base_url().'application/templates/images/icons/messages-read.png" border="0" class="icon_big" />
													<div class="page_header">'.$rs['subject'].'</div>
													<div>Між <a href="".$profileUrl."">'.$userName[0].' '.$userName[1].'</a> і Вами</div>
													<br />
													<br />
													
													<table cellpadding="0" cellspacing="0" width="100%">
													  <tr>
													  <td class="messages_view1" width="1">
													    <a href="'.$profileUrl.'"><img src="'.$userImage.'" border="0"  class="photo" width="60"></a>
													    <a name="bottom"></a>  </td>
													  <td class="messages_authorbox" nowrap="nowrap">
													    <div class="messages_author"><a href="'.$profileUrl.'">'.$userName[0].'<br />'.$userName[1].'</a></div>
													    <div class="messages_date">'.$sentDate.' @ '.$sentTime.'</div>
														'.$this->common->offline($rs['to_id'], 'message').'
													  </td>
													  <td class="messages_view2">'.parse_smileys(nl2br_except_pre($rs['message']), "http://demo.maax.ws/application/images/smileys/").'</td>
													  </tr>
													  <tr><td colspan="3">&nbsp;</td></tr>

													<tr>
													<td colspan="2">&nbsp;</td>
													<td class="messages_view2_bottom">
														
													  <div style="padding-top: 15px;">'.$backUrl.'</div>

													</td>
													</tr>
													</table>
													';
								}
							else
								{
									$backUrl	= '<a href="'.base_url().'messages/inbox">&larr; повернутись до отриманих</a>';
									mysql_query("UPDATE inbox_messages SET message_status='read' WHERE inbox_message_id='$msgId'");
									
									$rs	= getRow("inbox_messages","from_id,user_id,subject,message,datestamp","inbox_message_id='$msgId'");
									$userImage	= $this->common->getAvatar($rs['from_id']);
									$userName	= explode(" ", $this->common->getUsername($rs['from_id']));
									$actualDate	= strtotime($rs['datestamp']);
									$sentDate	= date('d.m.Y',$actualDate);
									$sentTime	= date('h:i',$actualDate);

									$data['action_id']	= $rs['from_id'];		//group id
									$data['action_type']= 'single_user';						//to indicate,message for group
									$data['action_name']= $this->common->getUsername($rs['from_id']);
									$data['action_url']	= base_url().'user/profile/'.$data['action_id'];
									$data['action_subject'] = $rs['subject'];
													
									$profileUrl	= base_url().'user/profile/'.$rs['from_id'];
									
									$content	= '
													<table class="tabs" cellpadding="0" cellspacing="0">
													<tr>
													<td class="tab0">&nbsp;</td>
													<td class="tab1" NOWRAP><a href="'.base_url().'messages/inbox">Отримані</a></td>
													<td class="tab">&nbsp;</td>
													<td class="tab2" NOWRAP><a href="'.base_url().'messages/sent">Надіслані</a></td>
													<td class="tab3">&nbsp;</td>
													</tr>
													</table>		
													
													<img src="'.base_url().'application/templates/images/icons/messages-read.png" border="0" class="icon_big" />
													<div class="page_header">'.$rs['subject'].'</div>
													<div>Між <a href="'.$profileUrl.'">'.$userName[0].' '.$userName[1].'</a> і Вами</div>
													<br />
													<br />
													
													<table cellpadding="0" cellspacing="0" width="100%">
													  <tr>
													  <td class="messages_view1" width="1">
														<div class="list2"><span><div><a href= '.$profileUrl.'><img src="'.$userImage.'" border="0"></a></div></span></div>
														</td>
													  <td class="messages_authorbox" nowrap="nowrap">
													    <div class="messages_author"><a href="'.$profileUrl.'">'.$userName[0].'<br />'.$userName[1].'</a></div>
													    <div class="messages_date">'.$sentDate.' @ '.$sentTime.'</div>
														'.$this->common->offline($rs['from_id'], 'message').'
													  </td>
													  <td class="messages_view2">'.parse_smileys(nl2br_except_pre($rs['message']), "http://demo.maax.ws/application/images/smileys/").'</td>
													  </tr>
													  <tr><td colspan="3">&nbsp;</td></tr>

													<tr>
													<td colspan="2">&nbsp;</td>
													<td class="messages_view2_bottom">
														
													  <div style="padding-top: 15px;">'.$backUrl.'</div>

													</td>
													</tr>
													</table>
													';
								}
							$data['read_message']	= true;
							$data['content']		= $content;
							$this->smartyextended->view('messages/inbox',$data);
						}//end if
					else
						header("Location: ".base_url()."messages/inbox");
				}
		}//end read

		
	function load_settings()
		{
			global $userId,$now,$adminName,$adminEmail,$siteName,$siteTitle;
			$this->load->model('common');
			
			$userId		= $this->session->userdata('user_id');
			$now		= $this->config->item('now');
			
			$siteVars	= $this->common->getSiteVars();
			$adminName	= $siteVars['adminName'];
			$adminEmail	= $siteVars['adminEmail'];
			$siteName	= $siteVars['siteName'];
			$siteTitle	= $siteVars['siteTitle'];
		}
}
?>
